package com.example.bili.controller;

import com.example.bili.entity.Trend;
import com.example.bili.service.TrendService;
import com.example.bili.util.CommonUtil;
import jakarta.annotation.Resource;
import org.apache.commons.io.FilenameUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;


/**
 * (Trend)表控制层
 *
 * @author makejava
 * @since 2025-02-24 14:27:27
 */
@RestController
@RequestMapping("/trend")
public class TrendController {
    /**
     * 服务对象
     */
    @Resource
    private TrendService trendService;

    @GetMapping("/getTrend")
    public Object getTrend() {
        return ResponseEntity.ok(trendService.getTrend());
    }


    @GetMapping("/getTrendByType")
    public Object getTrendByType(int userid) {
        return ResponseEntity.ok(trendService.getTrendByType(userid));
    }


    @GetMapping("/detail/{id}")
    public Trend getTrendDetail(
            @PathVariable("id") Integer id
    ) {
        // 这里可以添加业务逻辑，比如根据content参数过滤数据
        return trendService.getByid(id);
    }
    //分类推荐视频
    @GetMapping("/detail1/{typeid}")
    public Object getall(@PathVariable int typeid){
        return trendService.getall(typeid);
    }


    //    查询我关注的全部动态
    @GetMapping("/listTrendAll/{userId}")
    public Object listTrendAll(@PathVariable int userId){
        return trendService.listTrendAll(userId);
    }

    //    查询我关注的视频动态
    @GetMapping("/listTrend/{userId}")
    public Object listTrend(@PathVariable int userId){
        return trendService.listTrend(userId);
    }

    //  查询用户的全部动态
    @GetMapping("/getByuserId/{id}")
    public Object getByuserId(@PathVariable int id){
        return trendService.getByuserId(id);
    }

    //    发布动态
    @PostMapping("/addTrend")
    public Object addTrend(@RequestBody Trend trend){
        System.out.println(trend.toString());
        return trendService.addTrend(trend);
    }
    @PostMapping("/upimg")
    public Object upimg(MultipartFile fileobjec){
        if(fileobjec==null) return "";
        String upFilePath ="D:/代码/biliqian/public/imgaa";
        return upLoad(fileobjec,upFilePath);
    }

    //    查询动态的评论数量
    @GetMapping("/trendCmtNum/{id}")
    public Object trendCmtNum(@PathVariable int id){
        return trendService.trendCmtNum(id);
    }

    //    查询动态的点赞数量
    @GetMapping("/trendLikeNum/{id}")
    public Object trendLikeNum(@PathVariable int id){
        return trendService.trendLikeNum(id);
    }


    private String upLoad(MultipartFile upfile, String upFilePath) {
        //设置名字:原名，后缀，随机生成新名
        String yuanName = upfile.getOriginalFilename();
        //原名
        String surFix = FilenameUtils.getExtension(yuanName);
        //3.格式和文件大小的判断
        if (upfile.getSize()>1000000){
            throw new RuntimeException("文件太大了,1M以内");
        }
        else if (!CommonUtil.UP_FILE_ALLOW_TYPES.contains(surFix)){
            throw new RuntimeException("文件上传类型有误，可包含"+CommonUtil.UP_FILE_ALLOW_TYPES);
        }
        //4.上传
        String newName = "idpic-"+ UUID.randomUUID()+"."+surFix;

        //上传步骤
        File upFileObject = new File(upFilePath);
        if (!upFileObject.exists()){
            upFileObject.mkdirs();
        }
        try {
            upfile.transferTo(new File(upFileObject,newName));
            //数据库存储的路径
            return CommonUtil.UP_FILE_PATH+newName;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
    @GetMapping("/getTrenndByuserId/{userId}")
    public Object getTrenndByuserId(@PathVariable int userId){
        return trendService.getTrenndByuserId(userId);
    }
}

